﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StudentManagement.Extensions.Pagenation;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Models;
using StudentManagement.Services;
using StudentManagement.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StudentManagement.Controllers
{
    [Authorize]
    public class TeacherController : Controller
    {
        private readonly IRepository<Teacher> teacherRepository;
        private readonly ITeacherService teacherService;

        public TeacherController(IRepository<Teacher> teacherRepository, ITeacherService teacherService)
        {
            this.teacherRepository = teacherRepository;
            this.teacherService = teacherService;
        }

        /// <summary>
        /// list all
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(PageParameters pageParameters, int selectedId)
        {
            ViewBag.PageTitle = "教师列表";
            ViewBag.SortBy = pageParameters.SortBy.Trim();
            ViewBag.SearchString = pageParameters.SearchString?.Trim();

            var models = teacherService.GetPagedData(pageParameters);

            var viewModel = new TeacherListViewModel
            {
                Teachers = models,
                SelectedId = selectedId
            };

            // 如果教师Id不为空，获取该教师教授的课程
            if (selectedId != 0) {
                var teacher = models.DataSource.FirstOrDefault(t => t.Id == selectedId);
                viewModel.Courses = teacher.TeacherCourses.Select(a => a.Course).ToList();                
            }            

            return View(viewModel);
        }

        [HttpGet]
        public IActionResult Details(int id)
        {
            var teacher = teacherRepository.Get(id);
            if (teacher != null) {
                ViewBag.PageTitle = "教师信息详情页";
                return View(teacher);
            }
            else {
                Response.StatusCode = 404;
                return View("error");
            }
        }

        [HttpGet]
        public IActionResult Create()
        {
            ViewBag.PageTitle = "添加学生数据";
            return View();
        }

        [HttpPost]
        public IActionResult Create(Teacher teacher)
        {
            if (ModelState.IsValid) {
                teacherRepository.Insert(teacher);
                return RedirectToAction("Index");
            }

            ViewBag.PageTitle = "添加教师";
            return View();
        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            var teacher = teacherRepository.Get(id);
            ViewBag.PageTitle = "编辑教师数据";
            return View(teacher);
        }

        [HttpPost]
        public IActionResult Edit(Teacher teacher)
        {
            if (ModelState.IsValid) {
                teacherRepository.Update(teacher);
                return RedirectToAction("Index");
            }
            return View();
        }

        [HttpGet]
        public IActionResult Delete(int id)
        {
            var teacher = teacherRepository.Get(id);
            if (teacher != null) {
                teacherRepository.Delete(teacher);
                return RedirectToAction("Index");
            }

            return View();
        }
    }
}
